﻿#define _CRT_SECURE_NO_WARNINGS 1

/*
	猜数字游戏
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void menu()
{
	printf("*************************\n");
	printf("*****   1. play   *******\n");
	printf("*****   0. exit   *******\n");
	printf("*************************\n");
}

void game()
{
	int key = rand() % 100 + 1;
	printf("key = %d\n", key);
	int c = 0;
	int count = 5;
	printf("您一共有%d次机会！！！\n\n", count);

	while (1)
	{
		printf("请输入您要猜的数字：");
		scanf("%d", &c);
		if (c < key)
		{
			printf("您猜的数小了\n\n");
			count--;
		}
		else if (c > key)
		{
			printf("您猜的数大了\n\n");
			count--;
		}
		else
		{
			printf("恭喜您，您猜对了！\n\n");
			break;
		}
		if (count == 1)
		{
			printf("注意，您还有最后一次机会！！\a\a\a\n\n");
		}
		if (count == 0)
		{
			printf("您的次数已用完，挑战失败！\n\n");
			break;
		}
	}
}

int main()
{
	srand((unsigned int)time(NULL));
	int input = 1;
	
	while (input)
	{
		menu();
		printf("请输入：");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏！\n");
			break;
		default:
			printf("输入有误，请重新输入！\n");	
			break;
		}
	}
	return 0;
}
